<?php

/*
	Class: State

	About: Author
               artwells

	About: License
		<http://communit.as/docs/license>

	About: See Also
	 	<Cts_Db_Table_Abstract>
	 	Zend_Db_Table
*/
class State extends Cts_Db_Table_Abstract {

	/* Group: Protected Class Variables */

	/*
		Variable: $_name
			name of the table being used
	*/
    protected $_name = 'default_state';

    /*
		Variable: $_primary
			primary key of the table being used
	*/
    protected $_primary = array('locale', 'ckey');

	/* Group: Instance Methods */

	/*
		Function: keyExists
			Verifies the existence of a key in the configuration for a particular module.

		Arguments:
			module - The ID of the module to search. (the haystack)
			ckey - The key to search for. (the needle)

		Returns: boolean
	*/
    function keyExists($ckey,$locale='en-us') {
		$where = $this->getAdapter()->quoteInto("ckey = ?", $ckey);
		$where .= $this->getAdapter()->quoteInto(" and locale = ?", $locale);
		if ($this->getCountByWhereClause($where) == 0) {
			return false;
		} else {
			return true;
		}
	}

	/*
		Function: get

		Arguments:
			module - TBD
			ckey - TBD

		Returns: TBD
	*/
    function get($ckey,$locale='en-us') {
		$where = $this->getAdapter()->quoteInto("ckey = ?", $ckey);
		$where .= $this->getAdapter()->quoteInto(" and locale = ?", $locale);
		$value = $this->fetchRow($where);
		if (!is_null($value)) {
			return $value->value;
		} else {
			return null;
		}
	}

	/*
		Function: set

		Arguments: 
			module - TBD
			ckey - TBD
			value - TBD

		Returns: void
	*/
    function set($ckey, $value, $locale='en-us') {
      if (!$this->keyExists($ckey,$locale)) {
			$data = array(
				"ckey" 		=> $ckey,
				"locale"	=> $locale,
				"value"		=> $value,
				"created_on" => 'NOW()',
				"last_modified_on" => 'NOW()'
			);
			parent::insert($data);
		} else {
			$where = $this->getAdapter()->quoteInto("ckey = ?", $ckey);
			$where .= $this->getAdapter()->quoteInto(" and locale = ?", $locale);
			$data = array("value" => $value,
				"last_modified_on" => 'NOW()'
				      );

			parent::update($data, $where);
		}
	}
	
}
